package com.gs.weather.service.impl;


import com.github.pagehelper.PageHelper;
import com.gs.weather.dao.DisasterInfoDao;
import com.gs.weather.entity.DisasterInfo;
import com.gs.weather.service.DisasterInfoService;
import com.gs.weather.utils.Constant;
import com.gs.weather.utils.HttpUtils;
import com.gs.weather.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/9/27 0027
 */
@Service
public class DisasterInfoServiceImpl implements DisasterInfoService {
    @Autowired
    DisasterInfoDao disasterInfoDao;

    @Override
    public ResponseEntity addInfo(DisasterInfo info) {
        disasterInfoDao.addDisasterInfo(info);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }

    @Override
    public ResponseEntity updateInfo(DisasterInfo info) {
        disasterInfoDao.updateDisasterInfo(info);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }


    @Override
    public PageBean getAllInfo(String name, String time, Integer pageNum) {
        pageNum = pageNum == null ? 1 : pageNum;
        PageHelper.startPage(pageNum, Constant.PAGE_SIZE);
        List<DisasterInfo> infos = disasterInfoDao.queryAllDisasterInfosWithCondition(name, time);
        PageBean<DisasterInfo> result = new PageBean(infos);
        return result;
    }

    @Override
    public DisasterInfo getInfoById(long id) {
        DisasterInfo info = disasterInfoDao.queryDisasterInfoById(id);
        return info;
    }

    @Override
    public ResponseEntity deleteInfo(long id) {
        disasterInfoDao.deleteDisasterInfo(id);
        return HttpUtils.buildStringResponse("OK", HttpStatus.OK);
    }
}
